import sys
from PySide6 import QtCore, QtWidgets,QtCharts,QtSql
# from QtWidgets import  QApplication,QFileDialog
# from QtCore import  SIGNAL,QRect,QObject,SLOT
import ui_ApplicationWindow
from PySide6.QtCore import Slot
from dlgabout import dlgabout
from dlgstandard import dlgstandard
from dbdata import DBdata
import myglobal
# from PySide6.QtCharts import *
from numpy import arange,sin,cos
from dlgOpenSample import dlgOpenSample
from dlgtp import dlgtp
from DlgTableEdit import DlgTableEdit
class MyChartView(QtCharts.QChartView):
    def __init__(self,chart,parent=None) :
        super().__init__(chart,parent)
        self.m_isTouching=False
        self.setRubberBand(QtCharts.QChartView.RectangleRubberBand)
    def viewportEvent(self,event):
        if (event.type() == QtCore.QEvent.TouchBegin):
            # By default touch events are converted to mouse events. So
            # after this event we will get a mouse event also but we want
            # to handle touch events as gestures only. So we need this safeguard
            # to block mouse events that are actually generated from touch.
            self.m_isTouching = True

            # Turn off animations when handling gestures they
            # will only slow us down.
            # self.chart().setAnimationOptions(QtCharts.QChart.NoAnimation)
        return super().viewportEvent(event)

    def mousePressEvent(self,event):
        if (self.m_isTouching):
            return
        super().mousePressEvent(event)

    def mouseMoveEvent(self,event):
        if (self.m_isTouching):
            return;
        super().mouseMoveEvent(event);

    def mouseReleaseEvent(self,event):
        if (self.m_isTouching):
            self.m_isTouching = False;

        # Because we disabled animations when touch event was detected
        # we must put them back on.
        # self.chart().setAnimationOptions(QtCharts.QChart.SeriesAnimations);
        super().mouseReleaseEvent(event);
    def keyPressEvent(self,event):
        if(event.key()==QtCore.Qt.Key_Plus):
            self.chart().zoomIn();
        elif(event.key()==QtCore.Qt.Key_Minus):
            self.chart().zoomOut();
        elif(event.key()==QtCore.Qt.Key_Left):
            self.chart().scroll(-10, 0);
        elif(event.key()==QtCore.Qt.Key_Right):
            self.chart().scroll(10, 0);
        elif(event.key()==QtCore.Qt.Key_Up):
            self.chart().scroll(0, 10);
        elif(event.key()==QtCore.Qt.Key_Down):
            self.chart().scroll(0, -10);
        else:
            super().keyPressEvent(event);
class ApplicationWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)
        self.ui=ui_ApplicationWindow.Ui_MaMainWindowClass();
        self.ui.setupUi(self)

        self.model =QtSql.QSqlQueryModel(self)
        self.model.setQuery("select * from sample where sampleid in tmpid")
        self.ui.mytableview.setModel(self.model);

        myglobal.tp1.dataArrive.connect(self.tp_dataArrive)
        
        self.curve1 =  QtCharts.QLineSeries()
        # self.x = arange(0, 10.0, 10.0/27)
        # self.y1 = sin(self.x)
        # for i in range(len(self.x)):
        #     self.curve1.append(self.x[i], self.y1[i])

        self.chart = QtCharts.QChart()
        self.chart.addSeries(self.curve1)
        self.axis_x = QtCharts.QValueAxis()
        self.axis_x.setLabelFormat('%.1f')
        self.axis_x.setTitleText('X')
        self.axis_x.setTickCount(11)
        self.axis_x.setMinorTickCount(4)
        self.axis_y = QtCharts.QValueAxis()
        self.axis_y.setLabelFormat('%.1f')
        self.axis_y.setTitleText('Y')
        self.chart.addAxis(self.axis_x,QtCore.Qt.AlignBottom)
        self.chart.addAxis(self.axis_y, QtCore.Qt.AlignLeft)
        self.curve1.attachAxis(self.axis_x)
        self.curve1.attachAxis(self.axis_y)

        self.chartView=MyChartView(self.chart)
        # self.chartView.View.setChart(self.chart)
        self.i=0
        self.horizontalLayout =QtWidgets.QHBoxLayout(self.ui.graphicsView)
        self.horizontalLayout.addWidget(self.chartView)
        # self.ui.actionOpenSample.triggered.connect(self.on_actionOpenSample_triggered)
    @Slot()    
    def on_actionOpenSample_triggered(self):
        l=dlgOpenSample(self);
        l.exec();
        self.model.setQuery("select * from sample where sampleid in tmpid");
        pass
    @Slot()
    def on_actionClear_All_triggered(self):
        myglobal.db.clearAll()
        self.model.setQuery("select * from sample where sampleid in tmpid");
    @Slot()    
    def on_actionUsers_triggered(self):
        l=DlgTableEdit()
        l.setTableName("muser");
        l.exec();
    def tp_dataArrive(self,value):
        self.ui.lineEdit_weight.setText(str(value))
        self.i+=1
        # print(self.i,value)
        self.curve1.append(self.i,value)
    def handled1(self):
        pass        
    def handled1(self):
        pass
    @Slot()
    def on_actionStandards_triggered(self):
        l=dlgstandard(self);
        l.exec();
    @Slot()
    def on_actionAbout_triggered(self):
        # print("on_actionAbout_triggered");
        d=dlgabout()
        d.exec()
    @Slot()
    def on_actionExit_triggered(self):
        self.close() 
    @Slot()
    def on_actionBalance_triggered(self):
        print("on_tp_triggered");
        d=dlgtp()
        d.exec()        
    def chkLcClick(self):
        # print("on_actionAbout_triggered");
        d=dlgabout()
        d.exec()        
    def chkLsClick(self):
        # print("on_actionAbout_triggered");
        d=dlgabout()
        d.exec()        
    def chkHcClick(self):
        # print("on_actionAbout_triggered");
        d=dlgabout()
        d.exec()        
    def chkHsClick(self):
        # print("on_actionAbout_triggered");
        d=dlgabout()
        d.exec()            
if __name__ == "__main__":
    a = QtWidgets.QApplication(sys.argv)
    a.lastWindowClosed.connect(a.quit)
    w = ApplicationWindow()
    w.show()
    a.exec()